package cn.hncu.booksManagementSystem.user.ui;

import java.awt.Color;
import java.awt.Font;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.user.vo.UserModel;
import cn.hncu.booksManagementSystem.user.business.ebi.UserEbi;
import cn.hncu.booksManagementSystem.user.business.factory.UserEbiFactory;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;

public class ListPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JFrame mainFrame = null;
	private JList<UserModel> listUser;
	private JButton btnToQueryPanel;
	private JButton btnToUpdatePanel;
	private JButton btnDelete;
	private JButton btnToAddPanel;
	//注入ebi
	private UserEbi ebi = UserEbiFactory.getInstanceOfUserEbi();
	private JScrollPane scrollPane;
	/**
	 * Create the panel.
	 * @wbp.parser.constructor
	 */
	public ListPanel( JFrame mainFrame ) {
		this.mainFrame = mainFrame;
		initContentPane();
		initListUserData(ebi.getAllUser());//调用逻辑层
	}
	/**
	 * 通过 col 来初始化用户模块主界面
	 * @param mainFrame 主框架
	 * @param col 初始化列表的集合
	 */
	public ListPanel(JFrame mainFrame,Collection<UserModel> col) {
		this.mainFrame = mainFrame;
		initContentPane();
		initListUserData(col);
	}
	
	public void initContentPane(){
		setLayout(null);
		
		JLabel label = new JLabel("用户管理");
		label.setForeground(Color.RED);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(323, 34, 158, 43);
		add(label);
		
		btnToAddPanel = new JButton("添加...");
		btnToAddPanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new AddPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnToAddPanel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnToAddPanel.setBounds(502, 116, 140, 43);
		add(btnToAddPanel);
		
		btnDelete = new JButton("删除用户");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//1收集参数
				
				//2 组织参数
				UserModel user = listUser.getSelectedValue();
				if (user==null) {
					JOptionPane.showMessageDialog(null, "请选择你要删除的用户！");
					return;
				}
				//3调用逻辑层
				boolean boo = ebi.deleteUser(user);
				
				//4导向结果界面
				if (boo) {
					JOptionPane.showMessageDialog(null, "删除成功！");
					initListUserData(ebi.getAllUser());
				}else {
					JOptionPane.showMessageDialog(null, "删除失败！");
				}
			}
		});
		btnDelete.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnDelete.setBounds(502, 215, 140, 43);
		add(btnDelete);
		
		btnToUpdatePanel = new JButton("修改...");
		btnToUpdatePanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				//组织参数
				UserModel oldUser = listUser.getSelectedValue();
				if (oldUser==null) {
					JOptionPane.showMessageDialog(null, "请选择你要修改的用户！");
					return;
				}
				
				//导向结果界面
				mainFrame.setContentPane(new UpdatePanel(mainFrame, oldUser));
				mainFrame.validate();
			}
		});
		btnToUpdatePanel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnToUpdatePanel.setBounds(502, 309, 140, 43);
		add(btnToUpdatePanel);
		
		btnToQueryPanel = new JButton("查询...");
		btnToQueryPanel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new QueryPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnToQueryPanel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnToQueryPanel.setBounds(502, 412, 140, 43);
		add(btnToQueryPanel);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(143, 120, 273, 367);
		add(scrollPane);
		
		listUser = new JList<UserModel>();
		scrollPane.setViewportView(listUser);
		listUser.setFont(new Font("微软雅黑", Font.PLAIN, 24));
	}
	
	public void initListUserData( Collection<UserModel> col) {
		UserModel[] users = col.toArray(new UserModel[0]);
		listUser.setListData(users);
	}
	
}
